package com.fjec.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.shiro.AccountInfo;
import com.fjec.common.util.DateUtil;
import com.fjec.common.util.JsonUtil;
import com.fjec.market.entity.BizFavorite;
import com.fjec.market.entity.InfoSku;
import com.fjec.market.mapper.BizFavoriteMapper;
import com.fjec.market.mapper.InfoSkuMapper;
import com.fjec.market.service.IBizFavoriteService;
import org.apache.commons.collections.MapUtils;
import org.elasticsearch.common.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 018.收藏商品表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-09-16
 */
@Service
@Transactional
public class BizFavoriteServiceImpl extends ServiceImpl<BizFavoriteMapper, BizFavorite> implements IBizFavoriteService {

    @Resource
    private BizFavoriteMapper mapper;

    @Resource
    private InfoSkuMapper infoSkuMapper;

    @Override
    public JsonResult saveFavorite(Long skuId) {
        BizFavorite favorite = new BizFavorite();
        favorite.setUserId(AccountInfo.getUserId());
        favorite.setSkuId(skuId);
        favorite.setCreateTime(DateUtil.nowLdt());
        mapper.insert(favorite);
        return JsonResult.success()
                .code(EnumSysCode.MSG_6002.getCode())
                .message(EnumSysCode.MSG_6002.getMsg())
                .build();
    }

    @Override
    public JsonResult deleteBySkuId(Long skuId) {
        UpdateWrapper<BizFavorite> wrapper = new UpdateWrapper();
        wrapper.eq("UserId", AccountInfo.getUserId())
                .eq("SkuId", skuId);
        mapper.delete(wrapper);
        return JsonResult.success()
                .code(EnumSysCode.MSG_6004.getCode())
                .message(EnumSysCode.MSG_6004.getMsg())
                .build();
    }

    @Override
    public JsonResult favoriteList(Page page) {
        Page<Map<String, Object>> returnPage = infoSkuMapper.getFavoInfoSkuList(page, AccountInfo.getUserId());
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> tmpMap;
        for (Map<String, Object> entityMap : returnPage.getRecords()) {
            tmpMap = new HashMap<>();
            tmpMap.putAll(entityMap);
            tmpMap.remove("SpuPics");
            String strPics = MapUtils.getString(entityMap, "SpuPics");
            if (!Strings.isNullOrEmpty(strPics)) {
                List<String> picList = JsonUtil.jsonToObject(strPics, List.class);
                if (picList != null && !picList.isEmpty()) {
                    tmpMap.put("picUrl", picList.get(0));
                }
            }
            resultList.add(tmpMap);
        }
        return JsonResult.success()
                .total(returnPage.getTotal())
                .data(resultList)
                .build();
    }
}
